<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of welcome
 *
 * @author Obi
 */
class Member extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->m_member = new M_member();
        $this->m_kota = new M_kota();
        $this->m_provinsi = new M_provinsi();
        $this->m_pemesanan = new M_pemesanan();
        $this->m_konfirmasi_transfer = new M_konfirmasi_transfer();


        $this->m_pemesanan = new M_pemesanan();
        $this->m_pemesanandet = new M_pemesanan_det();

        $this->m_retur = new M_retur();
        $this->m_retur_det = new M_retur_det();
    }

    function index($alert = NULL) {

        $id = $this->session->userdata('id_member');

        $data['list_member'] = $this->m_member->where("id = " . $id)->get();

        $data['menu'] = array(
            $this->load->view('parsing/client/menu_right_member', '', true),
        );

        $data['title'] = "Halaman Member > Ubah Akun Login";
        $data['alert'] = $alert;

        $data['isi'] = 'isi_edit_akun_member';
        $this->load->view('template_client', $data);
    }

    function save_akun_login() {

        $id = $this->session->userdata('id_member');
        $old_password = md5($this->input->post('old_password'));
        $new_password = md5($this->input->post('new_password'));

        $dta = $this->m_member->where("id = " . $id)->get();

        if ($dta->password == $old_password) {
            $alert = "13";
            $data = array(
                'email' => $this->input->post('email'),
                'password' => $new_password,
            );
            $this->m_member->update($data, $id);
        } else {
            $alert = "12";
        }


        $this->index($alert);
    }

    function alamat_member($alert = NULL) {


        $data['menu'] = array(
            $this->load->view('parsing/client/menu_right_member', '', true),
        );

        $data['title'] = "Halaman Member > Ubah Alamat Pengguna";
        $data['alert'] = $alert;

        $id = $this->session->userdata('id_member');
        $data['list_member'] = $this->m_member->where("id = " . $id)->get();
        $data['list_prov'] = $this->m_provinsi->get();
        $data['list_kota'] = $this->m_kota->search_data($data['list_member']->id_provinsi);

        $data['isi'] = 'isi_alamat_akun_member';
        $this->load->view('template_client', $data);
    }

    function save_akun_alamat() {
        $data = array(
            'nama_depan' => $this->input->post('nama_depan'),
            'nama_belakang' => $this->input->post('nama_belakang'),
            'id_provinsi' => $this->input->post('provinsi'),
            'id_kota' => $this->input->post('kota'),
            'alamat' => $this->input->post('alamat'),
            'kode_pos' => $this->input->post('kode_pos'),
            'telp' => $this->input->post('telp'),
            'status' => 1,
        );

        $id = $this->session->userdata('id_member');

        if ($this->m_member->update($data, $id)) {
            $alert = "13";
        } else {
            $alert = "13";
        }


        $this->alamat_member($alert);
    }

    function konfirmasi_transfer($alert = NULL) {


        $data['menu'] = array(
            $this->load->view('parsing/client/menu_right_member', '', true),
        );

        $data['title'] = "Halaman Member > Konfirmasi Pembayaran ";
        $data['alert'] = $alert;

        $id_member = $this->session->userdata('id_member');
        $data['list_pemesanan'] = $this->m_pemesanan->select_private_data($id_member);

        $data['isi'] = 'isi_konfirmasi_transfer';
        $this->load->view('template_client', $data);
    }

    function save_konfirmasi_transfer() {
        $id_member = $this->session->userdata('id_member');
        $data = array(
            'id_pemesanan' => $this->input->post('order_id'),
            'tgl_transfer' => $this->input->post('tgl_transfer'),
            'nama_bank' => $this->input->post('nama_bank'),
            'nama_nasabah' => $this->input->post('nama_nasabah'),
            'nominal' => $this->input->post('nominal'),
            'tujuan_transfer' => $this->input->post('tujuan_transfer'),
            'id_member' => $id_member,
            'status' => 0,
        );

        $this->m_konfirmasi_transfer->insert($data);

        $alert = "13";
        $this->konfirmasi_transfer($alert);
    }

    function view_history_order($alert = NULL) {


        $data['menu'] = array(
            $this->load->view('parsing/client/menu_right_member', '', true),
        );

        $data['alert'] = $alert;
        $data['title'] = "Halaman Member > History Pemesanan";

        $id = $this->session->userdata('id_member');

        $data['list_pemesanan'] = $this->m_pemesanan->select_data_member($id);



        $data['isi'] = 'isi_history_pemesanan_member';
        $this->load->view('template_client', $data);
    }

    function view_history_order_det($id = NULL) {

        $data['title'] = " Keranjang Pemesanan";
        $data['keterangan'] = "Berikut ini adalah produk yang anda akan pesan.";

        $data['menu'] = array(
        );

        $data['list_data'] = $this->m_pemesanan->select_data_det($id);
        $data['list_data_det'] = $this->m_pemesanandet->detail_data($id);
        $data['isi'] = 'isi_history_pemesanan_member_det';
        $this->load->view('template_client', $data);
    }

    function form_retur($id = NULL) {

        $data['title'] = " Keranjang Pemesanan";
        $data['keterangan'] = "Berikut ini adalah produk yang anda akan pesan.";


        $data['menu'] = array(
        );

        $data['list_data'] = $this->m_pemesanan->select_data_det($id);
        $data['list_data_det'] = $this->m_pemesanandet->detail_data($id);
        $data['isi'] = 'isi_form_retur';
        $this->load->view('template_client', $data);
    }

    function save_retur() {
        $id_member = $this->session->userdata('id_member');

        $retur['id'] = date('YmdHis') . $id_member;
        $retur['tgl_retur'] = date('Y-m-d H:i:s');
        $retur['id_transaksi'] = $this->input->post('id_transaksi');
        $retur['status'] = "0";

        $this->m_retur->insert($retur);


        $pemesanan_det = $this->m_pemesanandet->where("id_pemesanan = '" . $retur['id_transaksi'] . "'")->get();
        $x = 0;
        foreach ($pemesanan_det as $dta) {
            $x++;
            $retur_det['id_pemesanan_det'] = $dta->id;
            $retur_det['bny'] = $this->input->post('bny_' . $x);
            $retur_det['alasan'] = $this->input->post('alasan_' . $x);
            $retur_det['id_retur'] = $retur['id'];
            $retur_det['status'] = "0";

            if ($this->input->post('bny_' . $x) > 0) {
                $this->m_retur_det->insert($retur_det);
            }

            $alert = "13";

            $this->view_history_retur($alert);
        }
    }

    function view_history_retur($alert = NULL) {

        $data['menu'] = array(
            $this->load->view('parsing/client/menu_right_member', '', true),
        );

        $data['title'] = "Halaman Member > History Retur Pemesanan";
        $data['list_retur'] = $this->m_retur->select_data();

        $id = $this->session->userdata('id_member');

        $data['alert'] = $alert;
        $data['isi'] = 'isi_history_retur_member';
        $this->load->view('template_client', $data);
    }

    function view_history_retur_det($id = NULL) {

        $data['title'] = " Keranjang Pemesanan";
        $data['keterangan'] = "Berikut ini adalah produk yang anda akan pesan.";

        $data['menu'] = array(
        );

        $data['list_data'] = $this->m_retur->select_data_det($id);
        $data['list_data_det'] = $this->m_retur_det->detail_data($id);
        $data['isi'] = 'isi_history_retur_member_det';
        $this->load->view('template_client', $data);
    }

    function cari_biaya($id = NULL) {
        $grand_total = 0;
        $dta = $this->m_pemesanan->where("id = '$id'")->get();
        echo $grand_total = $dta->grand_total;
    }

}

?>
